/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
#include <std.h>
#include <csl.h>
#include <csl_dat.h>
#include <csl_cache.h>
#include <chan.h>
#include <scom.h>
#include <utl.h>
#include <fvid.h>
#include <tsk.h>

#include <edc.h>
#include <vport.h>
#include <vportdis.h>
#include <saa7105.h>    

#include <evmdm642.h>                      
#include <evmdm642_disParamsSDTVDefault.h>


#include "appMain.h"
#include "appThreads.h"
#include "tskVideoOutput.h"
#include "prepost.h"


VPORT_PortParams EVMDM642_vDisParamsPort 
  = EVMDM642_DIS_PARAMS_PORT_DEFAULT;

#if defined _NTSC

static VPORTDIS_Params EVMDM642_vDisParamsChan 
  = EVMDM642_DIS_PARAMS_CHAN_BT656_DEFAULT(NTSC);

static SAA7105_ConfParams EVMDM642_vDisParamsSAA7105 
  = EVMDM642_DIS_PARAMS_SAA7105_SDTV_DEFAULT(NTSC720, SVIDEO);

#elif defined _PAL

static VPORTDIS_Params EVMDM642_vDisParamsChan 
  = EVMDM642_DIS_PARAMS_CHAN_BT656_DEFAULT(PAL);

static SAA7105_ConfParams EVMDM642_vDisParamsSAA7105 
  = EVMDM642_DIS_PARAMS_SAA7105_SDTV_DEFAULT(PAL720, SVIDEO);
#endif



ThrOutput thrOutput;

/* heap IDs defined in the BIOS configuration file */
extern int extHeap;

#pragma DATA_SECTION(scratch2,".internal_data1");
#pragma DATA_ALIGN(scratch2,8);
unsigned char scratch2[20*LINE_SZ];


FVID_Handle  disChan;
FVID_Frame *disFrameBuf;


/*-------------------------------------------------------*/
/* Initialize the display channel                        */
/*-------------------------------------------------------*/
void tskVideoOutputInit()
{
    int status;    

	EVMDM642_vDisParamsChan.segId = extHeap; //EXTERNALHEAP;
    EVMDM642_vDisParamsSAA7105.hI2C = EVMDM642_I2C_hI2C;
    
    /******************************************************/
    /* initialization of display driver                   */
    /******************************************************/
    disChan = FVID_create("/VP2DISPLAY", IOM_OUTPUT, 
        &status, (Ptr)&EVMDM642_vDisParamsChan, NULL);
        

    /******************************************************/
    /* configure video encoder & decoder                  */
    /******************************************************/
    FVID_control(disChan, VPORT_CMD_EDC_BASE + EDC_CONFIG, 
        (Ptr)&EVMDM642_vDisParamsSAA7105);



}


/*-------------------------------------------------------*/
/* Start the display channel                             */
/*-------------------------------------------------------*/
void tskVideoOutputStart() 
{
	FVID_control(disChan, VPORT_CMD_START, NULL);
}



/*-------------------------------------------------------*/
/* handle video display, scaling and re-format           */
/*-------------------------------------------------------*/
void tskVideoOutput()
{

    unsigned char   *frame;
    IMG in1,out1;
    SCRATCH scr;
    
    SCOM_Handle fromProctoOut,fromOuttoProc;
	ScomBufChannels *pMsgBuf; 
    fromProctoOut = SCOM_open("PROCTOOUT");
	fromOuttoProc = SCOM_open("OUTTOPROC");
	FVID_alloc(disChan, &disFrameBuf);

 	while(1)
 	{
		/*-----------------------------------------------------------*/
  		/* Wait for the message from the process task to recieve new */
  		/* frame to be displayed.                                    */
   		/*-----------------------------------------------------------*/
 		pMsgBuf = SCOM_getMsg(fromProctoOut, SYS_FOREVER);
        frame = (unsigned char *)pMsgBuf->bufChannel;
            		   
 		in1.Y_data  = (unsigned char*)(*(int*)(frame+0));
 		in1.Cb_data = (unsigned char*)(*(int*)(frame+4));
		in1.Cr_data = (unsigned char*)(*(int*)(frame+8));

		out1.Y_data = (unsigned char *)disFrameBuf->frame.iFrm.y1;
		out1.Cb_data = (unsigned char *)disFrameBuf->frame.iFrm.cr1;
		out1.Cr_data = (unsigned char *)disFrameBuf->frame.iFrm.cb1;

 		scr.in_data = scratch2;	
 		CACHE_clean(CACHE_L2ALL,NULL,NULL);

 		/*-----------------------------------------------------------*/
 		/* Call YUV420to422v to do chroma resampling                 */
 		/*-----------------------------------------------------------*/
 		YUV420to422v(&in1,&out1,720,MAX_NUM_LINES_PER_FIELD*2,&scr);	
		
 		CACHE_clean(CACHE_L2ALL,NULL,NULL);

       	/*-----------------------------------------------------------*/
       	/* Display the decoded frame.                                */
      	/*-----------------------------------------------------------*/
	    FVID_exchange(disChan, &disFrameBuf);
  
 	  	/*-----------------------------------------------------------*/
   		/* Send message to process task to continue                  */
   		/*-----------------------------------------------------------*/
 	  	SCOM_putMsg(fromOuttoProc, pMsgBuf);

	}	    

}
